<div class="form">
    <?php echo CHtml::beginForm() ?>
    <?php
    $screen = Blyn::app()->getUIManager()->getCurrentScreen();
    $cityList = Blyn::app()->getActiveApp()->getCityList();
    $model = new BDynamicFormModel();
    $model->addField('findText');
    $model->addField('userCity');
    //by default, set user city to ChongQing
    $model->userCity = 'china.chongqing';

    $savedUserCity = Blyn::app()->getCurrentUser()->getAttribute('myCity_app' . Blyn::app()->getActiveApp()->getId());
    if ($savedUserCity instanceof BAttribute) {
        $model->userCity = Blyn::app()->getCurrentUser()->getAttribute('myCity_app' . Blyn::app()->getActiveApp()->getId())->getValue();
    }

    if ($_POST != NULL && isset($_POST['BDynamicFormModel'])) {
        $model->attributes = $_POST['BDynamicFormModel'];
    }

    if (!$savedUserCity instanceof BAttribute || $savedUserCity->getValue() != $model->userCity) {
        Blyn::app()->getCurrentUser()->addAttribute('myCity_app' . Blyn::app()->getActiveApp()->getId(), $model->userCity);
    }
    ?>
    <div class="row-fluid">
        <div class="span8"></div>
        <div >
            <strong>你所在城市：</strong><?php echo CHtml::activeDropDownList($model, 'userCity', $cityList, array('class' => 'span2')); ?>
        </div>
    </div>
    <div class="row">
        <?php echo CHtml::activeTextField($model, 'findText'); ?>
        <?php //echo $screen->getAjaxLink(array('name' => '通过场地商查找', 'id' => 'searchByVender'), array('htmlOptions' => array('class' => 'btn'))); ?>
        <?php //echo $screen->getAjaxLink(array('name' => '通过场地类型查找', 'searchByCourtType'), array('htmlOptions' => array('class' => 'btn'))); ?>
        <?php echo $screen->getAjaxLink(array('name' => '快速查找', 'id' => 'fastSearch'), array('htmlOptions' => array('class' => 'btn'))); ?>
        <?php echo $screen->getAjaxLink(array('name' => '更新并查找', 'id' => 'search'), array('htmlOptions' => array('class' => 'btn'))); ?>
    </div>
    <?php echo CHtml::endForm() ?>
</div>

<?php
$app = Blyn::app()->getActiveApp();
$availableList = array();
$findText = NULL;

if ($screen->isAjaxLinkClicked('search')) {
    $findText = $model->findText;
    if ($findText != NULL) {
        $availableList = $app->findByFindText($findText, array('findType' => 'full'));
    } else {
        $app->findByFindText($findText, array('findType' => 'full'));
    }
}
if ($screen->isAjaxLinkClicked('fastSearch')) {
    $findText = $model->findText;
    if ($findText != NULL)
        $availableList = $app->findByFindText($findText);
}
?>
<?php if (!empty($availableList)) { ?>
    <?php
    $control1 = new BUIControl('showFindList');
    $control1->addRenderView('showFindList', 'booking_list', array('availableList' => $availableList, 'findText' => $findText));
    $control1->render();
    //$control->renderView('booking_list', array('availableList' => $availableList, 'findText' => $findText)); 
    ?>
    <?php
} else {
    $control2 = new BUIControl('showFindSummary');
    $control2->addRenderView('booking_find_summary');
    $control2->render();
}
?>
